package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Setmeal;
import com.itheima.pojo.User;
import com.itheima.service.SetMealService;
import com.itheima.service.UserService;
import com.itheima.utils.fileNameUtils;
import com.itheima.utils.QiniuUtils;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.List;
import java.util.UUID;


@RestController
@RequestMapping("/setMeal")
public class SetMealController {
    @Autowired
    private JedisPool jedisPool;

    @Reference
    private SetMealService setMealService;

    @RequestMapping("upload.do")
    public Result upload(MultipartFile imgFile) throws IOException {

        try {
            String path = "http://localhost:8080/fileServer/upload/";
            String filename = imgFile.getOriginalFilename();
            String name = fileNameUtils.getName(filename);

            Client client = Client.create();
            WebResource resource = client.resource(path + name);

            resource.put(imgFile.getBytes());

            Jedis jedis = jedisPool.getResource();
            jedis.sadd(RedisConstant.SETMEAL_PIC_RESOURCES,name);

            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS, name);
        } catch (Exception e) {
            e.printStackTrace();
            e.printStackTrace();
            e.printStackTrace();
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }


    }



    //文件上传
//    @RequestMapping("/upload")
//    public Result upload(MultipartFile imgFile){
//        System.out.println(imgFile);
//        String originalFilename = imgFile.getOriginalFilename();//原始文件名 3bd90d2c-4e82-42a1-a401-882c88b06a1a2.jpg
//        int index = originalFilename.lastIndexOf(".");
//        String extention = originalFilename.substring(index - 1);//.jpg
//        String fileName = UUID.randomUUID().toString() + extention;//	FuM1Sa5TtL_ekLsdkYWcf5pyjKGu.jpg
//        try {
//            //将文件上传到七牛云服务器
//            QiniuUtils.upload2Qiniu(imgFile.getBytes(),fileName);
//            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,fileName);
//        } catch (IOException e) {
//            e.printStackTrace();
//            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
//        }
//        return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,fileName);
//    }



    @RequestMapping("add.do")
    public Result add( int[] id,@RequestBody Setmeal setmeal) {

        try {
            setMealService.add(id,setmeal);

            if (setmeal.getImg()!=null){
                Jedis jedis = jedisPool.getResource();
                jedis.sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg());
            }
            return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_SETMEAL_FAIL);
        }
    }


    @RequestMapping("findPage.do")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {

        PageResult pageResult=setMealService.findPage(queryPageBean);
        return pageResult;
    }


    @RequestMapping("findAllSetMeal.do")
    public Result findAllSetMeal() {

        try {
            List<Setmeal> list = setMealService.findAllSetMeal();
            return new Result(true, MessageConstant.GET_SETMEAL_LIST_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.GET_SETMEAL_LIST_FAIL);
        }
    }




}
